package com.example.spring.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.spring.demo.common.model.vo.TotalVo;
import com.example.spring.demo.entity.Goods;
import com.example.spring.demo.entity.GoodsReport;
import com.example.spring.demo.service.GoodsReportService;
import com.example.spring.demo.service.GoodsService;
import com.example.spring.demo.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2025/1/1 9:19
 **/
@RequestMapping("/api/home")
@RestController
public class HomeController {
    @Autowired
    private GoodsReportService goodsReportService; //举报
    @Autowired
    private WxUserService wxUserService; //用户
    @Autowired
    private GoodsService goodsService; //商品
    @GetMapping("/getTotal")
    public TotalVo getTotal() { //首页数据
        TotalVo vo = new TotalVo();
        //查询待处理数量
        QueryWrapper<GoodsReport> query = new QueryWrapper<>();
        query.lambda().eq(GoodsReport::getStatus, "0"); //待处理
        int doTotal = (int) goodsReportService.count(query);
        vo.setDoTotal(doTotal);
        //会员总数
        int userCount = (int) wxUserService.count();
        vo.setUserTotal(userCount);
        //闲置总数
        QueryWrapper<Goods> uquery = new QueryWrapper<>();
        uquery.lambda().eq(Goods::getType, "0");//闲置
        int uncount = (int) goodsService.count(uquery);
        vo.setUnusedTotal(uncount);
        //返回数据
        return vo;
    }
    //查询投诉列表
    @GetMapping("/getDoReport")
    public List<GoodsReport> getDoReport() {
        QueryWrapper<GoodsReport> query = new QueryWrapper<>();
        query.lambda().eq(GoodsReport::getStatus, "0")
                .orderByDesc(GoodsReport::getReportTime)
                .last(" limit 8");
        List<GoodsReport> list = goodsReportService.list(query);
        if(!list.isEmpty()){
            for (GoodsReport goodsReport : list) {
                Goods goods = goodsService.getById(goodsReport.getGoodsId());
                goodsReport.setGoodsName(goods.getGoodsName());
            }
        }
        return list;
    }
}